import * as articletype from '@/api/articletype'

const state = {
	noticeList: [],
	codeMap: {},
    articleType: []
}
const mutations = {
	// 设置全局通知列表数据
	setNoticeList(state, list) {
		console.log('setNoticeList')
		state.noticeList = list
	},
    setArticleType(state, params) {
        state.articleType = params
    }
}

const getters = {
	getNoticeList(state) {
		return state.noticeList
	},
}

const actions = {
	getArticleTypeListByCode({ state }, code) {
		return new Promise((resolve, reject) => {
			const codeValue = state.articleType
			if (state.articleType.length) {
                const findResult = state.articleType.find((row) => {
                    if (row.colCode == code) {
                        return row
                    }
                })
                if (findResult) {
                    resolve(findResult)
                } else {
                    throw new Error('未找到分类:' + code)
                }
			} else {
				articletype
					.getTypeListAll()
					.then((res) => {
						const data = res.data
						if (data.code == 200 && data.rows.length) {
                            state.setArticleType(data.rows)
							const findResult = data.rows.find((row) => {
								if (row.colCode == code) {
									return row
								}
							})
							if (findResult) {
								resolve(findResult)
							} else {
								throw new Error('未找到分类:' + code)
							}
						} else {
							throw new Error('未找到分类:' + code)
						}
					})
					.catch((error) => {
						reject(error)
					})
			}
		})
	},
}

export default {
	namespaced: true,
	state,
	mutations,
	actions,
	getters,
}
